//
//  HWWuKongForumDetailVC.swift
//  HWanGame
//
//  Created by Apple on 2024/8/6.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView

class HWWuKongDetailVC: HWBaseViewController {
    
    private let backButton = c_make_ex(UIButton.self)
        .image("new_back").end()
    private let bgImageView = c_make_ex(UIImageView.self)
        .image("Wukong_bg").mode(.scaleAspectFill).end()
    ///标签
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationView.isHidden = true
        view.backgroundColor = .colorWithHexString(hexString: "#E8E9EB")
        
        view.subviews{
            bgImageView
            backButton
            segmentedView
        }

        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["游戏","评论", "论坛", "攻略"]
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.white.alpha(0.5)
        segmentedDataSource.titleSelectedColor = UIColor.white
        segmentedDataSource.titleNormalFont = .systemFont(ofSize: 16*widthScale, weight: .regular)
        segmentedDataSource.titleSelectedFont = .systemFont(ofSize: 16*widthScale, weight: .semibold)
        segmentedDataSource.itemSpacing = 11*widthScale
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = .white
        indicator.indicatorHeight = 2*widthScale
        indicator.indicatorWidth = 28*widthScale
        indicator.indicatorCornerRadius = 2*widthScale
        indicator.verticalOffset = 5*widthScale
        segmentedView.indicators = [indicator]
        
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        initFrame()
        loadData()
    }
    
    private func initFrame() {

        bgImageView.fillContainer()
        segmentedView.leading(56*widthScale).top(CGFloat.statusHeight).height(44*widthScale).width(200*widthScale)
        backButton.leading(0).top(.statusHeight).width(56*widthScale).height(44*widthScale)
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom)
        }
        
    }
    
    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
}

//MARK: JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate
extension HWWuKongDetailVC:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {

        if index == 1 {
            let comment = HWGameDetailCommentController()
            comment.gameId = "584"
            return comment
        }
        return HWWuKongDetailListVC()
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
        if index == 2 || index == 3 {
            let community = HWGameCommunityController()
            community.communityId = "96"
            self.navigationController?.pushViewController(community, animated: true)
            self.segmentedView.selectItemAt(index: 0)
        }
        
    }
    

}

//MARK: data
extension HWWuKongDetailVC {
    
    func loadData(){
        
        self.segmentedDataSource.titles = ["游戏","评论", "论坛", "攻略"]
        self.segmentedView.reloadData()
    }
}

